package com.jichangxiu.framework.entity.bo;

import cn.hutool.core.util.ObjectUtil;
import com.jichangxiu.common.entity.rule.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SecurityUser implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    private TenantRule tenantRule;

    private UserRule userRule;

    private List<SecurityPost> securityPostList;

    private List<SecurityRole> securityRoleList;

    private List<SecurityPower> securityPowerList;

    public String getTenantId() {
        return tenantRule.getTenantId();
    }

    public String getTenantCode() {
        return tenantRule.getTenantCode();
    }

    public String getTenantName() {
        return tenantRule.getTenantName();
    }

    public Boolean isPlatform() {
        return tenantRule.getIsPlatform();
    }

    public String getTenantSource() {
        return tenantRule.getTenantSource();
    }

    public String getUserId() {
        return userRule.getUserId();
    }

    public String getUserCode() {
        return userRule.getUserCode();
    }

    public String getNickName() {
        return userRule.getNickName();
    }

    public String getLoginName() {
        return userRule.getLoginName();
    }

    public String getLoginPwd() {
        return userRule.getLoginPwd();
    }

    public String getDeptId() {
        return userRule.getDeptId();
    }

    public String getDeptName() {
        return userRule.getDeptName();
    }

    public List<PostRule> getPostRuleList() {
        List<SecurityPost> securityPostList = getSecurityPostList();
        if (ObjectUtil.isEmpty(securityPostList)) return new ArrayList<>();
        return securityPostList.stream().map(SecurityPost::getPostRule).collect(Collectors.toList());
    }

    public List<String> getPostCodeList() {
        List<PostRule> postRuleList = getPostRuleList();
        if (ObjectUtil.isEmpty(postRuleList)) return new ArrayList<>();
        return postRuleList.stream().map(PostRule::getPostCode).collect(Collectors.toList());
    }

    public List<RoleRule> getRoleRuleList() {
        List<SecurityRole> securityRoleList = getSecurityRoleList();
        if (ObjectUtil.isEmpty(securityRoleList)) return new ArrayList<>();
        return securityRoleList.stream().map(SecurityRole::getRoleRule).collect(Collectors.toList());
    }

    public List<String> getRoleCodeList() {
        List<RoleRule> roleRuleList = getRoleRuleList();
        if (ObjectUtil.isEmpty(roleRuleList)) return new ArrayList<>();
        return roleRuleList.stream().map(RoleRule::getRoleCode).collect(Collectors.toList());
    }

    public List<PowerRule> getPowerRuleList() {
        List<SecurityPower> securityPowerList = getSecurityPowerList();
        if (ObjectUtil.isEmpty(securityPowerList)) return new ArrayList<>();
        return securityPowerList.stream().map(SecurityPower::getPowerRule).collect(Collectors.toList());
    }

    public List<String> getPowerCodeList() {
        List<PowerRule> powerRuleList = getPowerRuleList();
        if (ObjectUtil.isEmpty(powerRuleList)) return new ArrayList<>();
        return powerRuleList.stream().map(PowerRule::getPowerCode).collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return userRule.getLoginPwd();
    }

    @Override
    public String getUsername() {
        return userRule.getLoginName();
    }

    @Override
    public boolean isAccountNonExpired() {
        // 账户是否过期：
        // 过期：false
        // 未过期：true
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 账号是否锁定
        // 锁定：false
        // 未锁定：true
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 凭证是否过期
        // 过期：false
        // 未过期：true
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 账号是否启用
        // 启用：true
        // 未启用：false
        return true;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(this.securityPostList))
            grantedAuthorities.addAll(this.securityPostList);
        if (ObjectUtil.isNotEmpty(this.securityRoleList))
            grantedAuthorities.addAll(this.securityRoleList);
        if (ObjectUtil.isNotEmpty(this.securityPowerList))
            grantedAuthorities.addAll(this.securityPowerList);
        return grantedAuthorities;
    }

}
